//do ...\Process_046_Logit_MatchMatlab_2008_2012_v3_w_seed.do 44000 16 v11_robustb	

//Call the files created by: do ...\MakeAttribute_struct_demo_BLP_robustd_choiceset_2008_2012_v11_heter_set_seed_bs_11022017.do and create file that can be used to estimated a conditional logit in Stata. 

clear all
set more off
set mem 10000m
pause on
  
global pathname="raw_data_folder"
global censuspath="raw_data_folder"
global sales_tax_path="raw_data_folder"

local sample_size 20


local subsample 			  `"`1'"'         
local group_id_selected       `"`2'"' 	     //Income category to select: 16
local version   			  `"`3'"'  		 //v11_11022017_robustb
local set_seed 				  `"`4'"'		 //1
 


//Step 1: Create Choice Set and Price Time Series 
//-----------------------------------------------------
 	 

use $pathname\refrigerators\pid_id_2008_2012_v11022017_struct_`subsample'_`group_id_selected'_seed_`set_seed', clear 
	sort pid
save $pathname\refrigerators\pid_id_2008_2012_v11022017_struct_`subsample'_`group_id_selected'_seed_`set_seed', replace  	


//Create a weekly file 
use $pathname\refrigerators\choiceset_trimester_week_zipcode_2008_2012_struct_`version'_`subsample'_`group_id_selected'_seed_`set_seed', clear
		//This merge is only to replace pid by pid_id
		sort pid
     	merge pid using $pathname\refrigerators\pid_id_2008_2012_v11022017_struct_`subsample'_`group_id_selected'_seed_`set_seed'
	 	tab _m
	 	keep if _m==3
	 	drop _m

		keep zipcode pid_id year week
    	sort zipcode week year
save $pathname\refrigerators\choiceset_trimester_week_zipcode_2008_2012_struct_`version'_`subsample'_`group_id_selected'_seed_`set_seed'_4joinby, replace
	
//Step 2: Joinby random sample and choice sets
//------------------------------------------------

use  $pathname\refrigerators\lcidemo_046_2008_2012_struct_`version'_`subsample'_`group_id_selected'_seed_`set_seed', clear



drop brand_id standard_class type_id
capture drop delisted2010 delisted

//Determine the choice set
	sort pid
     	merge pid using $pathname\refrigerators\pid_id_2008_2012_v11022017_struct_`subsample'_`group_id_selected'_seed_`set_seed'
	 	tab _m
	 	keep if _m==3
	 	drop _m 
 	drop pid
	ren pid_id pid_bought

	sort zipcode week year
	joinby zipcode week year using  $pathname\refrigerators\choiceset_trimester_week_zipcode_2008_2012_struct_`version'_`subsample'_`group_id_selected'_seed_`set_seed'_4joinby
	
 	gen choice=0
 	replace choice=1 if pid_id==pid_bought
 	
 	bysort hd_id:egen choice_sum=sum(choice)
	bysort hd_id:gen choice_size=_N
	bysort pid_id:gen nb_sales=_N
	//Some households have choice_sum=0 because they were sampled outside the period for which the balanced panel data was constructed
	keep if choice_sum==1  
	//drop if choice_size<5
	//drop if nb_sales<2
	bysort hd_id:egen choice_sum2=sum(choice)

	keep if choice_sum2==1  
	
	sort pid_id zipcode year week
 	//merge pid zipcode year week using $pathname\refrigerators\price_week_zip_046_2008
 	merge pid_id zipcode year week using $pathname\refrigerators\price_mean_046_2008_2012_v11022017_struct_`subsample'_`group_id_selected'_seed_`set_seed'_vLong
  	tab _m
 
  	//Some prices are missing because we are using the weekly data, while the choice set is imputed using the monthly data.
 	keep if _m==3
 	drop _m

 	
//Step 3: Attributes, rebates, electricity prices
//-----------------------------------------------------
	
 	//Merging with attributes:
 	preserve

			use  $pathname\refrigerators\attributes_2008_2012_weekly_tri, clear
				sort pid
	     		merge pid using $pathname\refrigerators\pid_id_2008_2012_v11022017_struct_`subsample'_`group_id_selected'_seed_`set_seed'
		 		tab _m
		 		keep if _m==3
		 		drop _m
		 		drop pid
				sort pid_id year week	
			save  $pathname\refrigerators\attributes_2008_2012_weekly_tri_`subsample'_`group_id_selected'_seed_`set_seed', replace
		restore
	 	
 		sort pid_id  year week
		merge  pid_id year week  using $pathname\refrigerators\attributes_2008_2012_weekly_tri_`subsample'_`group_id_selected'_seed_`set_seed'
		tab _m
		drop if _m==2
		drop _m

	//Electricity prices at the state level
		sort state year
  		merge state year using $pathname\electricity\electricity_price_state_2007_2012
  		tab _m
  		tab state if _m==1
		tab year if _m==1
  		drop if _m==2
  		drop _m
	
  	//Electricity prices at the county level
  		sort zipcode
		merge zipcode using $censuspath\mapping_zip_county_nov99_short
		tab _m
		drop if _m==2
		drop _m
		ren county5 county_utility
  	
		sort county_utility year
  		merge county_utility year using "$pathname\electricity\county_elec_price_2007_2012"
		tab _m
  			
  		drop if _m==2
  		drop _m
  		replace pcount=p_elec if pcount==.
  				
	//Rebates
		sort state week
 		sort county_utility year week
		merge county_utility year week using "$pathname\rebate\DSIRE_rebate_week_county_2007_2013"  
		tab _m
		drop if _m==2
		drop _m
		ren incentive incentive_utility
		mvencode incentive_utility,mv(0) over 
		
		sort state year week
		merge state year week using "$pathname/rebate/Cash4Appliances/cash4appliance_refrigerators_weekly_vf_tmp"
		tab _m
		drop if _m==2
		ren _m merge_rebate
		ren incentive incentive_cfa
		mvencode incentive_cfa,mv(0) over 
		gen amount=incentive_utility+incentive_cfa		
		
		sort state
		merge state using "$pathname/rebate/Cash4Appliances/ProgramCharacteristics_Ref_CFA"
		tab _m
		drop if _m==2
		drop _m
				
save $pathname\refrigerators\lcidemo_046_households_2008_2012_`subsample'_`group_id_selected'_seed_`set_seed'_EstReady_robust3.dta, replace





